package com.drcurd.controller;

import com.drcurd.entity.Department;
import com.drcurd.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/departments")
@CrossOrigin
public class DepartmentController {
    
    @Autowired
    private DepartmentService departmentService;
    
    // 查询所有部门
    @GetMapping
    public Map<String, Object> getAllDepartments() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Department> departments = departmentService.getAllDepartments();
            result.put("success", true);
            result.put("data", departments);
            result.put("message", "查询成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败: " + e.getMessage());
        }
        return result;
    }
    
    // 根据ID查询部门
    @GetMapping("/{id}")
    public Map<String, Object> getDepartmentById(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            Department department = departmentService.getDepartmentById(id);
            if (department != null) {
                result.put("success", true);
                result.put("data", department);
                result.put("message", "查询成功");
            } else {
                result.put("success", false);
                result.put("message", "部门不存在");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败: " + e.getMessage());
        }
        return result;
    }
    
    // 新增部门
    @PostMapping
    public Map<String, Object> addDepartment(@RequestBody Department department) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = departmentService.addDepartment(department);
            if (success) {
                result.put("success", true);
                result.put("message", "新增成功");
            } else {
                result.put("success", false);
                result.put("message", "新增失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "新增失败: " + e.getMessage());
        }
        return result;
    }
    
    // 更新部门
    @PutMapping("/{id}")
    public Map<String, Object> updateDepartment(@PathVariable Long id, @RequestBody Department department) {
        Map<String, Object> result = new HashMap<>();
        try {
            department.setId(id);
            boolean success = departmentService.updateDepartment(department);
            if (success) {
                result.put("success", true);
                result.put("message", "更新成功");
            } else {
                result.put("success", false);
                result.put("message", "更新失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "更新失败: " + e.getMessage());
        }
        return result;
    }
    
    // 删除部门
    @DeleteMapping("/{id}")
    public Map<String, Object> deleteDepartment(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = departmentService.deleteDepartment(id);
            if (success) {
                result.put("success", true);
                result.put("message", "删除成功");
            } else {
                result.put("success", false);
                result.put("message", "删除失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "删除失败: " + e.getMessage());
        }
        return result;
    }
    
    // 分页查询
    @GetMapping("/page")
    public Map<String, Object> getDepartmentsByPage(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Department> departments = departmentService.getDepartmentsByPage(page, size);
            int total = departmentService.getDepartmentCount();
            result.put("success", true);
            result.put("data", departments);
            result.put("total", total);
            result.put("page", page);
            result.put("size", size);
            result.put("message", "查询成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败: " + e.getMessage());
        }
        return result;
    }
    
    /**
     * 根据条件搜索部门
     */
    @GetMapping("/search")
    public Map<String, Object> searchDepartments(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String sn,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        Map<String, Object> result = new HashMap<>();
        try {
            com.drcurd.dto.DepartmentQueryDTO queryDTO = new com.drcurd.dto.DepartmentQueryDTO();
            queryDTO.setName(name);
            queryDTO.setSn(sn);
            queryDTO.setPageNum(page);
            queryDTO.setPageSize(size);
            
            com.github.pagehelper.PageInfo<Department> pageInfo = departmentService.getDepartmentListByCondition(queryDTO);
            result.put("success", true);
            result.put("data", pageInfo.getList());
            result.put("total", pageInfo.getTotal());
            result.put("page", page);
            result.put("size", size);
            result.put("message", "查询成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败：" + e.getMessage());
        }
        return result;
    }
} 